package com.oldroid.apps.getit.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.oldroid.apps.getit.R;
import com.oldroid.apps.getit.util.AnalyticsUtils;


/**
 * Faq Activity
 * @author Jervis Muindi
 *
 */
public class FaqActivity extends BaseActivity{
	private static final String TAG = "FaqActivity";
	private Activity mActivity = this; 
	static TextView details;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		AnalyticsUtils.getInstance(this).trackPageView("/FaqActivity");
		setContentView(R.layout.faq);
		getActivityHelper().setupActionBar(null, 0);
		
		
		loadFAQ();
	}
	
	
	private void loadText(){
		
		LinearLayout ll = (LinearLayout)findViewById(R.id.faq_root_container);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		
		TextView header = new TextView(this,null,R.style.TextHeader2); 
		header.setLayoutParams(lp);
		header.setText("Test header from code");
		
		TextView body = new TextView(this,null,R.style.TextBody);
		body.setLayoutParams(lp);
		body.setText("Test content from code");
		
		header = (TextView) getLayoutInflater().inflate(R.layout.faq_header_item, null);
		body = (TextView) getLayoutInflater().inflate(R.layout.faq_body_item, null);
		
		ll.addView(header);
		ll.addView(body);//
		
		
		
	}
	
	
	/**
	 * Adds a question and an answer to the current view in the
	 * FAQ
	 * @param question the question
	 * @param answer the answer 
	 */
	private void loadQuestion(String question, String answer){
		
		LinearLayout ll = (LinearLayout)findViewById(R.id.faq_root_container);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		
		TextView qn =  (TextView) getLayoutInflater().inflate(R.layout.faq_header_item, null); 
		qn.setLayoutParams(lp);
		qn.setText(question);
		
		TextView ans = (TextView) getLayoutInflater().inflate(R.layout.faq_body_item, null);
		ans.setLayoutParams(lp);
		ans.setText(answer);
		 
		ll.addView(qn);
		ll.addView(ans);
		
	}
	
	
	/**
	 * Loads the FAQ text from file and shows it in the UI
	 */
	private void  loadFAQ(){
		
		try {
			InputStream in = getAssets().open("faq.txt");
			
			String file = convertToString(in);
			
			String [] tokens = file.split("<DELIM>\\n"); // <DELIM> is a delimeter that separates questions from answers.. 
			
			for(int i =0; i < tokens.length; i = i+2){
				
				
				
				String qn = tokens[i];
				String ans = tokens [i+1];
				
				Log.d(TAG, "Q: " + qn);
				Log.d(TAG, "A: " + ans);
				
				// last item
				if(i == tokens.length - 2){
					System.out.println("");
				}
				
				//load the questions
				loadQuestion(qn, ans);
				
			}
			
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			String[] paths = null;
			try {
				paths = getAssets().list("./");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			int count = 1;
			for(String s : paths){
				System.err.println(count++ + ") s");
			}
			
		}
		
	}
	
	/**
	 * Convert a file input stream to its string representation
	 * @param in the input stream
	 * @return the string representation
	 */
	private String convertToString(InputStream in){
		
		Scanner input = new Scanner(in);
		
		String output = "";
		String ls = System.getProperty("line.separator");
		
		while(input.hasNextLine()){
			
			String line = input.nextLine();
			output += line + ls; 
		}
		
		return output; 
	}
	
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		getActivityHelper().setupHomeActivity();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.refresh_menu_items, menu);
		super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.menu_refresh){
			// need to implement refresh probably not
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	
	
	

}
